<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class ZlFortuneYearFeedback extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_fortune_year_feedback';

    protected $cacheModel = 'fortune.user_fortune_year_feedback';

    /**
     * 获取年运反馈信息
     * @param int $rid
     * @param int $year
     * @return array
     * @throws \Exception
     */
    public function getFortuneYearFeedbackInfo(int $rid, int $year, int $ver = 0)
    {
        $feedbackInfo = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $rid . ':' . $year)->getDataAndSet(function () use ($rid, $year)
        {
            try
            {
                $data = $this->where(['rid' => $rid, 'year' => $year])->field('id,uid,select1,select2,select3,content,create_time')->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        });

        if ($feedbackInfo)
        {
            $tagIds = array_values(array_filter([$feedbackInfo['select1'] ?? 0, $feedbackInfo['select2'] ?? 0, $feedbackInfo['select3'] ?? 0]));
            $tagInfos = (new ZlFeedbackTag())->batchGetInfoByIds($tagIds, ['name']);
            $optionSelected = [];

            foreach ($tagIds as $tagId)
            {
                if (isset($tagInfos[$tagId]) && $tagInfos[$tagId]['name'])
                {
                    $optionSelected[] = $tagInfos[$tagId]['name'];
                }
            }

            return [
                'option_selected' => $optionSelected,
                'content' => $feedbackInfo['content'] ?? '',
                'date' => date('Y年m月d日', $feedbackInfo['create_time']),
            ];
        } else
        {
            return ['option_list' => $this->getFortuneYearFeedbackTags($ver)];
        }
    }

    /**
     * 获取年运反馈tags信息
     * @return array
     * @throws \Exception
     */
    public function getFortuneYearFeedbackTags($ver): array
    {
        if ($ver)
        {
            $data = [
                [
                    'title' => '1.年运运势带给您的感觉是？',
                    'tag_type' => 3,
                ],
                [
                    'title' => '2.年运曲线带给你的感觉是？',
                    'tag_type' => 4,
                ],
                [
                    'title' => '3.重点天象带给你的感觉是？',
                    'tag_type' => 6,
                ],
            ];
        } else
        {
            $data = [
                [
                    'title' => ' * 年运运势带给您的感觉是？',
                    'tag_type' => 3,
                ],
                [
                    'title' => ' * 年运曲线带给您的感觉是？',
                    'tag_type' => 4,
                ],
                [
                    'title' => ' * 阶段运势带给您的感觉是？',
                    'tag_type' => 5,
                ],
            ];
        }
        $tagModel = new ZlFeedbackTag();
        $resData = [];
        foreach ($data as $datum)
        {
            $optionList = [];
            $tags = $tagModel->getTags($datum['tag_type'], ['id', 'name']);

            foreach ($tags as $tagItem)
            {
                $optionList[] = [
                    'id' => (int)$tagItem['id'],
                    'option_name' => $tagItem['name'],
                ];
            }

            $resData[] = [
                'title' => $datum['title'],
                'option' => $optionList,
            ];
        }

        return $resData;
    }

}
